﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SDATA;
using SDLL;

namespace SABI
{
    public partial class frm0001F : Form
    {
        public frm0001F()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            (Application.OpenForms["frmHome"] as frmHOME).CloseTab(this);
        }

        private void frm0001F_Load(object sender, EventArgs e)
        {

        }

        private void frm0001F_Shown(object sender, EventArgs e)
        {
            cboQuantityDecimal.SelectedIndex = 2;
            cboUnitCostDecimal.SelectedIndex = 2;
            cboConvertedDecimal.SelectedIndex = 2;
            foreach (Control ct in frameBox.Controls)
                if (ct.GetType() == typeof(ComboBox))
                    ((ComboBox)ct).SelectedIndex = 0;

            cboQuantityDecimal.SelectedItem = FRM.QuantityDecimals;
            cboUnitCostDecimal.SelectedItem = FRM.UnitCostDecimals;
            cboConvertedDecimal.SelectedItem = FRM.ConvertedDecimals;
            chkMessageQuantityAF3013.Checked = FRM.MessageQuantityAF3013;
            chkMessageQuantityAF3016.Checked = FRM.MessageQuantityAF3016;
            chkMessageQuantityAF4016.Checked = FRM.MessageQuantityAF4016;
            chkMessageQuantityAF4017.Checked = FRM.MessageQuantityAF4017;
            chkMessageUnitPriceAF3013.Checked = FRM.MessageUnitPriceAF3013;
            chkMessageUnitPriceAF3016.Checked = FRM.MessageUnitPriceAF3016;
            chkMessageUnitPriceAF4016.Checked = FRM.MessageUnitPriceAF4016;
            chkMessageUnitPriceAF4017.Checked = FRM.MessageUnitPriceAF4017;

            chkPhoneIsNumber.Checked = FRM.PhoneIsNumber;
            chkAutoCompactString.Checked = FRM.AutoCompactString;

            cboObjectNameConstraint.SelectedIndex = FRM.ObjectNameConstraint;
            cboTradeNameConstraint.SelectedIndex = FRM.TradeNameConstraint;
            cboVATNoConstraint.SelectedIndex = FRM.VATNoConstraint;
            cboAddressConstraint.SelectedIndex = FRM.AddressConstraint;
            cboTelConstraint.SelectedIndex = FRM.TelConstraint;
            cboMobileConstraint.SelectedIndex = FRM.MobileConstraint;
            cboFaxConstraint.SelectedIndex = FRM.FaxConstraint;
            cboEmailConstraint.SelectedIndex = FRM.EmailConstraint;
            cboWebsiteConstraint.SelectedIndex = FRM.WebsiteConstraint;
            cboContactorConstraint.SelectedIndex = FRM.ContactorConstraint;

            cboObjectNameAllowNull.SelectedIndex = FRM.ObjectNameAllowNull;
            cboTradeNameAllowNull.SelectedIndex = FRM.TradeNameAllowNull;
            cboVATNoAllowNull.SelectedIndex = FRM.VATNoAllowNull;
            cboAddressAllowNull.SelectedIndex = FRM.AddressAllowNull;
            cboTelAllowNull.SelectedIndex = FRM.TelAllowNull;
            cboMobileAllowNull.SelectedIndex = FRM.MobileAllowNull;
            cboFaxAllowNull.SelectedIndex = FRM.FaxAllowNull;
            cboEmailAllowNull.SelectedIndex = FRM.EmailAllowNull;
            cboWebsiteAllowNull.SelectedIndex = FRM.WebsiteAllowNull;
            cboContactorAllowNull.SelectedIndex = FRM.ContactorAllowNull;

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            FRM.QuantityDecimals = cboQuantityDecimal.SelectedItem.ToString();
            FRM.UnitCostDecimals = cboUnitCostDecimal.SelectedItem.ToString();
            FRM.ConvertedDecimals = cboConvertedDecimal.SelectedItem.ToString();
            FRM.MessageQuantityAF3013 = chkMessageQuantityAF3013.Checked;
            FRM.MessageQuantityAF3016 = chkMessageQuantityAF3016.Checked;
            FRM.MessageQuantityAF4016 = chkMessageQuantityAF4016.Checked;
            FRM.MessageQuantityAF4017 = chkMessageQuantityAF4017.Checked;
            FRM.MessageUnitPriceAF3013 = chkMessageUnitPriceAF3013.Checked;
            FRM.MessageUnitPriceAF3016 = chkMessageUnitPriceAF3016.Checked;
            FRM.MessageUnitPriceAF4016 = chkMessageUnitPriceAF4016.Checked;
            FRM.MessageUnitPriceAF4017 = chkMessageUnitPriceAF4017.Checked;

            FRM.PhoneIsNumber = chkPhoneIsNumber.Checked;
            FRM.AutoCompactString = chkAutoCompactString.Checked;

            FRM.ObjectNameConstraint = cboObjectNameConstraint.SelectedIndex;
            FRM.TradeNameConstraint = cboTradeNameConstraint.SelectedIndex;
            FRM.VATNoConstraint = cboVATNoConstraint.SelectedIndex;
            FRM.AddressConstraint = cboAddressConstraint.SelectedIndex;
            FRM.TelConstraint = cboTelConstraint.SelectedIndex;
            FRM.MobileConstraint = cboMobileConstraint.SelectedIndex;
            FRM.FaxConstraint = cboFaxConstraint.SelectedIndex;
            FRM.EmailConstraint = cboEmailConstraint.SelectedIndex;
            FRM.WebsiteConstraint = cboWebsiteConstraint.SelectedIndex;
            FRM.ContactorConstraint = cboContactorConstraint.SelectedIndex;

            FRM.ObjectNameAllowNull = cboObjectNameAllowNull.SelectedIndex;
            FRM.TradeNameAllowNull = cboTradeNameAllowNull.SelectedIndex;
            FRM.VATNoAllowNull = cboVATNoAllowNull.SelectedIndex;
            FRM.AddressAllowNull = cboAddressAllowNull.SelectedIndex;
            FRM.TelAllowNull = cboTelAllowNull.SelectedIndex;
            FRM.MobileAllowNull = cboMobileAllowNull.SelectedIndex;
            FRM.FaxAllowNull = cboFaxAllowNull.SelectedIndex;
            FRM.EmailAllowNull = cboEmailAllowNull.SelectedIndex;
            FRM.WebsiteAllowNull = cboWebsiteAllowNull.SelectedIndex;
            FRM.ContactorAllowNull = cboContactorAllowNull.SelectedIndex;

            string sql = "UPDATE tb0000 SET ";
            sql += string.Format("QuantityDecimals='{0}', UnitCostDecimals='{1}', ConvertedDecimals='{2}',", FRM.QuantityDecimals, FRM.UnitCostDecimals, FRM.ConvertedDecimals);
            sql += string.Format("MessageQuantityAF3013='{0}',MessageQuantityAF3016='{1}', MessageQuantityAF4016='{2}', MessageQuantityAF4017='{3}',",FRM.MessageQuantityAF3013?1:0,FRM.MessageQuantityAF3016?1:0,FRM.MessageQuantityAF4016?1:0,FRM.MessageQuantityAF4017?1:0);
            sql += string.Format("MessageUnitPriceAF3013='{0}', MessageUnitPriceAF3016='{1}', MessageUnitPriceAF4016='{2}', MessageUnitPriceAF4017='{3}',", FRM.MessageUnitPriceAF3013 ? 1 : 0, FRM.MessageUnitPriceAF3016 ? 1 : 0, FRM.MessageUnitPriceAF4016 ? 1 : 0, FRM.MessageUnitPriceAF4017 ? 1 : 0);
            sql += string.Format("PhoneIsNumber='{0}', AutoCompactString='{1}',", FRM.PhoneIsNumber ? 1 : 0, FRM.AutoCompactString ? 1 : 0);
            sql += string.Format("ObjectNameConstraint='{0}', TradeNameConstraint='{1}', VATNoConstraint='{2}', AddressConstraint='{3}', TelConstraint='{4}',", FRM.ObjectNameConstraint, FRM.TradeNameConstraint, FRM.VATNoConstraint, FRM.AddressConstraint,FRM.TelConstraint);
            sql += string.Format("MobileConstraint='{0}', FaxConstraint='{1}', EmailConstraint='{2}', WebsiteConstraint='{3}', ContactorConstraint='{4}',", FRM.MobileConstraint, FRM.FaxConstraint, FRM.EmailConstraint, FRM.WebsiteConstraint, FRM.ContactorConstraint);
            sql += string.Format("ObjectNameAllowNull='{0}', TradeNameAllowNull='{1}', VATNoAllowNull='{2}', AddressAllowNull='{3}', TelAllowNull='{4}',", FRM.ObjectNameAllowNull, FRM.TradeNameAllowNull, FRM.VATNoAllowNull, FRM.AddressAllowNull, FRM.TelAllowNull);
            sql += string.Format("MobileAllowNull='{0}', FaxAllowNull='{1}', EmailAllowNull='{2}', WebsiteAllowNull='{3}', ContactorAllowNull='{4}'", FRM.MobileAllowNull, FRM.FaxAllowNull, FRM.EmailAllowNull, FRM.WebsiteAllowNull, FRM.ContactorAllowNull);

            if (DATA.ExecuteNonQuery(sql, FRM.ConnectString, true))
                DLL02.Info("Cập nhật dữ liệu thành công!");
            else
                DLL02.Alert("Cập nhật dữ liệu thất bại!");


        }

    }
}
